﻿package com.floorplanner.lib3ds
{
	import com.floorplanner.lib3ds.*;
	
	public class Lib3dsTextureMap extends Lib3dsCore
	{
		public static const DECALE:uint       = 0x0001;
		public static const MIRROR:uint       = 0x0002;
		public static const NEGATE:uint       = 0x0004;
		public static const NO_TILE:uint      = 0x0008;
		public static const SUMMED_AREA:uint  = 0x0010;
		public static const ALPHA_SOURCE:uint = 0x0020;
		public static const TINT:uint         = 0x0040;
		public static const IGNORE_ALPHA:uint = 0x0080;
		public static const RGB_TINT:uint     = 0x0100;
  
		public var name:String; // [64];
		public var flags:int;
		public var percent:Number;
		public var blur:Number;
		public var scale:Array;
		public var offset:Array;
		public var rotation:Number;
		public var tint_1:Array;
		public var tint_2:Array;
		public var tint_r:Array;
		public var tint_g:Array;
		public var tint_b:Array;
		
		public function Lib3dsTextureMap()
		{
			this.name = "";
			this.flags = 0;
			this.blur = 0.0;
			this.scale = [1, 1, 1];
			this.offset = [0, 0, 0];
			this.rotation = 0.0;
		}
		
		/**
		 * 
		 * @param	io
		 * @return
		 */
		public function read( io:Lib3dsIO ):Boolean
		{
			var c:Lib3dsChunk = Lib3dsChunk.read( io );
			var chunk:uint = c.next( io );
			
			while( chunk != 0 )
			{
				switch( chunk )
				{
					case Lib3dsChunk.INT_PERCENTAGE:
						this.percent = io.readPercentage();
						debug("\tpercent: "  + this.percent);
						break;
					case Lib3dsChunk.MAT_MAPNAME:
						this.name = io.readString();
						debug("\tname: "  + this.name);
						break;
					case Lib3dsChunk.MAT_MAP_TILING:
						this.flags = io.readWord();
						debug("\tflags: "  + this.flags);
						break;
					case Lib3dsChunk.MAT_MAP_TEXBLUR:
						this.blur = io.readFloat();
						debug("\tblur: "  + this.blur);
						break;
					case Lib3dsChunk.MAT_MAP_USCALE:
						scale.x = io.readFloat();
						debug("\tscale U: "  + scale.z);
						break;
					case Lib3dsChunk.MAT_MAP_VSCALE:
						scale.y = io.readFloat();
						debug("\tscale V: "  + scale.y);
						break;
					case Lib3dsChunk.MAT_MAP_UOFFSET:
						offset.x = io.readFloat();
						debug("\toffset U: "  + offset.x);
						break;
					case Lib3dsChunk.MAT_MAP_VOFFSET:
						offset.y = io.readFloat();
						debug("\toffset V: "  + offset.y);
						break;
					case Lib3dsChunk.MAT_MAP_ANG:
						this.rotation = io.readFloat();
						debug("\tangle: "  + this.rotation);
						break;
					case Lib3dsChunk.MAT_MAP_COL1:
						this.tint_1 = new Array();
						this.tint_1[0] = io.readByte();
						this.tint_1[1] = io.readByte();
						this.tint_1[2] = io.readByte();
						debug("\ttint_1: "  + this.tint_1);
						break;
					case Lib3dsChunk.MAT_MAP_COL2:
						this.tint_2 = new Array();
						this.tint_2[0] = io.readByte();
						this.tint_2[1] = io.readByte();
						this.tint_2[2] = io.readByte();
						debug("\ttint_2: "  + this.tint_2);
						break;
					case Lib3dsChunk.MAT_MAP_RCOL:
						this.tint_r = new Array();
						this.tint_r[0] = io.readByte();
						this.tint_r[1] = io.readByte();
						this.tint_r[2] = io.readByte();
						debug("\ttint_r: "  + this.tint_r);
						break;
					case Lib3dsChunk.MAT_MAP_BCOL:
						this.tint_g = new Array();
						this.tint_g[0] = io.readByte();
						this.tint_g[1] = io.readByte();
						this.tint_g[2] = io.readByte();
						debug("\ttint_g: "  + this.tint_g);
						break;
					case Lib3dsChunk.MAT_MAP_GCOL:
						this.tint_b = new Array();
						this.tint_b[0] = io.readByte();
						this.tint_b[1] = io.readByte();
						this.tint_b[2] = io.readByte();
						debug("\ttint_b: "  + this.tint_b);
						break;
					default:
						debug("readTexture => unknown node 0x" + chunk.toString(16) );
						break;
				}
				chunk = c.next( io );
			}
			
			io.position = c.end;
			
			return true;
		}
	}
}
